﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FXnaGis;
using FXnaInfrastructure;

using ASWPF;

using ASCore;

using System.Windows.Forms;



namespace ASApplication
{
    public class GisMapFunctionListener
    {

        public GisMapFunctionListener(FMap map)
        {

            this._map = map;

            this.Configure();

           





        }



        FMap _map;


        FSimpleGeometryEditMF _simpleGeometryMF;

        public FSimpleGeometryEditMF SimpleGeometryMF
        {
            get { return _simpleGeometryMF; }

        }

        FRectMF _rectMF;

        public FRectMF RectMF
        {
            get { return _rectMF; }
           
        }

        FCircleMF _circleMF;

        public FCircleMF CircleMF
        {
            get { return _circleMF; }
            
        }


        FCubeMF _cubeMF;

        public FCubeMF CubeMF
        {
            get { return _cubeMF; }
          
        }


        FMoveMF _moveMF;

        public FMoveMF MoveMF
        {
            get { return _moveMF; }
           
        }




        protected void Configure()
        {

            //配置命令

            //动态配置右键命令菜单

            FSelectMF select = (FSelectMF)this._map.FunctionService.GetMapFunction(typeof(FSelectMF));

            ToolStripMenuItem mi = new ToolStripMenuItem();
            mi.Text = "编辑";

            mi.Click += new EventHandler(mi_Click);

            select.ContextMenuStrip.Items.Add(mi);







            //测试可能使用的移动命令

            this._moveMF = new FMoveMF(this._map.GeoMapFrame);

            this._moveMF.SelectedShapeMoved += new EventHandler<MoveEventArgs>(_moveMF_SelectedShapeMoved);

            this._map.FunctionService.AddMapFunction(this._moveMF);





            this._simpleGeometryMF = new FSimpleGeometryEditMF(this._map.GeoMapFrame);

            this._simpleGeometryMF.AskAddGeometry += new EventHandler(_simpleGeometryMF_AskAddGeometry);


            this._map.FunctionService.AddMapFunction(this._simpleGeometryMF);





            this._rectMF = new FRectMF(this._map.GeoMapFrame);
            


            this._map.FunctionService.AddMapFunction(this._rectMF);



            this._circleMF = new FCircleMF(this._map.GeoMapFrame);

            this._map.FunctionService.AddMapFunction(this._circleMF);



            this._cubeMF = new FCubeMF(this._map.GeoMapFrame);

            this._map.FunctionService.AddMapFunction(this._cubeMF);




 
        }

        void mi_Click(object sender, EventArgs e)
        {
            //winPointSource ps = new winPointSource();
            //ps.ShowDialog();
            
        }

        void _moveMF_SelectedShapeMoved(object sender, MoveEventArgs e)
        {


            //尝试建立平等化的模块更新体系，尤其是基于图形，坐标的一些属性，可以尝试着 异步更新

            //比如移动操作，至于添加和删除操作，则在之后我再想想。


            List<FVectorLayer> layers = this._map.GeoMapFrame.LayerService.GetVectorLayers();


            //循环所有图层的选择项,作为测试，我们先移动物体

            this._map.GeoMapFrame.SuspendEvents();



            List<string> ids = new List<string>();

            foreach (FVectorLayer vlayer in layers)
            {
                
                    foreach (FShape shape in vlayer.SelectedShapes)
                    {

                        ids.Add(shape.Id);

                        shape.Move(e.XOffset,e.YOffset,0);

                        
                    }
               
                
            }


            this._map.GeoMapFrame.ResumeEvents();


            //取得所有的污染源ID后，一次性移动

            //ProjectService.CurrentService.Project.ASData.LnSources.up

            

        }


        void _simpleGeometryMF_AskAddGeometry(object sender, EventArgs e)
        {
            switch (this._simpleGeometryMF.Mode)
            {

                case FSimpleGemotryEditMode.POINT:


                    this.AddPoint();


                    break;

                case FSimpleGemotryEditMode.LINE:


                    this.AddLine();


                    break;
                case FSimpleGemotryEditMode.POLY:


                    this.AddPoly();

                    break;
                default:
                    break;
            }
        }




        protected void AddPoint()
        {


            //需要区分是哪一个图层，在做处理

            string layerName = this._map.GeoMapFrame.LayerService.SelectedLayer.Id;

            switch (layerName)
            {
                case ASCoreNames.PointSourceLayer:


                     //SourceCollection scollection = ProjectService.CurrentService.Project.ASData.PtSources;

                    FVectorLayer layer = ProjectService.CurrentService.Project.ASData.PtLayer;

                    int idIndex = layer.IdGenerator.GetNextIndex();

                    string id = "PointSource" + idIndex.ToString();
                    string desc = "点源" + idIndex.ToString();

                    PointSource ps = new PointSource(id, desc);

                    ps.InnerShape.Point.Coordinate.X  = this._simpleGeometryMF.Coordinates[0].X;

                    ps.InnerShape.Point.Coordinate.Y = this._simpleGeometryMF.Coordinates[0].Y;





                    //ProjectService.CurrentService.Project.ASData.PtSources.AddSource(ps);

                    //FPointShape fps = new FPointShape(id, this._simpleGeometryMF.Coordinates[0]);
                    //fps.UserData = ps;


                    layer.AddShape(ps);



                    //在这里我们尝试另一个理论，不在这个时候弹出窗体，类似canada/a

                    //winPointSource win = new winPointSource(ps);
                    //if (win.ShowDialog() == true)
                    //{

                    //    ProjectService.CurrentService.Project.ASData.PtSources.AddSource(win.Ps);


                    //}




                    break;

                default :
                    break;

            }
           

           


 

        }


        protected void AddLine()
        {

            //FVectorLayer lnCollection = ProjectService.CurrentService.Project.ASData.LnLayer;

            //int idIndex = lnCollection.IdGenerator.GetNextIndex();

            //string id = "LineSource" + idIndex.ToString();
            //string desc = "线源" + idIndex.ToString();

            //LineSource ls = new LineSource(id, desc);

          


            //FLineShape fls = new FLineShape(id, this._simpleGeometryMF.Coordinates);

            //fls.UserData = ls;

            //lnCollection.AddShape(fls);


            //winLineSource win = new winLineSource(ls);
            //if (win.ShowDialog() == true)
            //{

            //    ProjectService.CurrentService.Project.ASData.LnSources.AddSource(win.Ls);


            //}

 
        }

        protected void AddPoly()
        {
             //需要区分是哪一个图层，在做处理

            string layerName = this._map.GeoMapFrame.LayerService.SelectedLayer.Id;

            switch (layerName)
            {
                case ASCoreNames.PolySourceLayer:

                    
                    // FVectorLayer scollection = ProjectService.CurrentService.Project.ASData.PolyLayer;

                    //int idIndex = scollection.IdGenerator.GetNextIndex();

                    //string id = "PolySource" + idIndex.ToString();
                    //string desc = "多边形面源" + idIndex.ToString();

                    //PolySource ps = new PolySource(id, desc);

                    //FPolyShape fps = new FPolyShape(id,this._simpleGeometryMF.Coordinates);
                    //fps.Desc = desc;

                    //fps.UserData = ps;


                    //scollection.AddShape(fps);


                  

                    //winPolySource win = new winPolySource(ps);
                    //if (win.ShowDialog() == true)
                    //{

                    //    ProjectService.CurrentService.Project.ASData.PolySource.AddSource(win.PolySource);


                    //}


                    break;

                default:
                    break;
            }
 

        }


    


    }
}
